﻿namespace WinnerCMS.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    
    using System.Linq;
    using System.Text;
    using WinnerCMS.Common;
    
    using WinnerCMS.Model;

    /// <summary>
    /// 咨询
    /// </summary>
    public class ConsultantDal  
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        public void Add(Consultant model)
        {
            using (Entities db = new Entities())
            {
                db.Consultants .Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 显示咨询
        /// </summary>
        /// <param name="top"></param>
        /// <param name="modelId"></param>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public IList<Consultant> ConsultantShow(int top, byte modelId, int contentId)
        {
            using (Entities db = new Entities())
            {
                return db.Consultants.Where(x => x.ModelId == modelId && x.ContentId == contentId).Take(top).ToList();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        { 
            using (Entities db = new Entities())
            {
                var q = from x in db.Consultants  where Ids.Contains(x.Id) select x;
                q.ToList().ForEach(x => db.Consultants .Remove(x));

                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 回复
        /// </summary>
        /// <param name="model"></param>
        public void Reply(Consultant model)
        {
            using (Entities db = new Entities())
            {
                var info = db.Consultants.Find(model.Id);

                info.ReplyContent = model.ReplyContent;
                info.ReplyTime = model.ReplyTime;
                info.Replier = model.Replier;

                db.SaveChangesAsync().Wait();
            }
             
        }
        /// <summary>
        /// 加载回复
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Consultant ReplyLoad(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.Consultants.Find(Id);
            }
        }

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Consultant> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Consultants
                            join y in db.Goods on x.ContentId equals y.Id
                            select x;
                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }

    }
}